/**
 * Handles all view windows with commands
 * from Controller
 */
package View;


import Model.CaptainsLog;
import Model.SolarSystem;
import Model.TradeGood;

import java.util.HashMap;

import javax.swing.JFrame;

/**
 * @author Asa Freedman
 *@version 1.0
 */
public class GUI extends JFrame {
	
	/**
	 * Player Window
	 */
	private PlayerWindow pw;
	
	/**
	 * Captain's log object
	 */
	private CaptainsLog log;

	/**
	 * Constructor for the GUI
	 * Showing splashscreen at first instance
	 */
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * Pops up the splash screen
	 * 
	 * @param save whether or not a saved game exists on the users
	 * @return int representing the button pressed
	 */
	public int splashPop(boolean save) {
		final SplashWindow splashWindow = new SplashWindow(save);
		getContentPane().add(splashWindow);
		validate();
		pack();
		setTitle("Welcome to Space Trader");
		int spw = splashWindow.getSplashChoice();
		while (spw < 0) {
			try {
				Thread.sleep(500);
				} 
			catch(InterruptedException ex) { 
				ex.printStackTrace();
				}
			spw = splashWindow.getSplashChoice();
		}

		getContentPane().remove(splashWindow);
		//invalidate();
		return spw;
	}
	
	/**
	 * Creates the player window which will 
	 * get player information prior to starting the game
	 * 
	 * @return Object Array containing the system
	 */
	public Object[] getPlayerInfo() {
		final MakePlayerWindow playerWindow = new MakePlayerWindow(); 
		getContentPane().add(playerWindow);
		validate();
		pack();
		setTitle("Make Your Character");
		while (playerWindow.getCharComplete() < 1) {
			try {
				Thread.sleep(500);
				} 
			catch(InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		final Object[] output = playerWindow.getInfo();
		getContentPane().remove(playerWindow);
		return output;
	}
	
	/**
	 * Pops up the player window.  
	 * The users window to the game.  Contains buttons for managing
	 * all the users interactions to the game.
	 * 
	 * @param fuel amount of current fuel
	 * @param fuelCap amount of fuel possible
	 * @param techLevel Tech level of current planet
	 * @param log Captain's log
	 * @return the value of the button pressed
	 */
	public int playerPop(int fuel, int fuelCap, int techLevel, CaptainsLog log) {
		this.log = log;
		getContentPane().removeAll();
		validate();
		pw = new PlayerWindow(fuel, fuelCap, techLevel, log);
		getContentPane().add(pw);
		setTitle("Your Cockpit");
		pack();
		repaint();
		while (pw.getButtonPressed() < 0) {
			try {
				Thread.sleep(500);
			} 
			catch(InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		return pw.getButtonPressed();
	}
	
	/**
	 * Pops up a map of the universe
	 * 
	 * @param ss array of planets in the solar system
	 * @param pl current player location
	 * @param shipFuel amount of fuel currently in the ship
	 * @param shipFuelCap maximum amount of fuel the ship can hold
	 * @return button returned from the warp screen
	 */
	public int[] warpPop(SolarSystem[][] ss, int[] pl, int shipFuel, int shipFuelCap) {
		getContentPane().removeAll();
		validate();
		final WarpWindow ww = new WarpWindow(ss, pl, shipFuel, shipFuelCap);
		getContentPane().add(ww);
		pack();
		setTitle("Map of the Universe");
		while (ww.getButtonPressed()[0] == -1) {
			try {
				Thread.sleep(500);
			} 
			catch(InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		return ww.getButtonPressed();
	}
	
	/**
	 * Pops up the market screen
	 * 
	 * @param marketMap a hashmap with the contents of this planets goods
	 * @param cargoSize the size of the players current ships' cargo hold
	 * @param playerMoney amount of money owned by the player
	 * @param onship goods on ship
	 * @return hashMap of the new market status
	 */
	public HashMap<String,Object> marketPop
		(HashMap<String,TradeGood> marketMap, HashMap<String,TradeGood> onship, 
			int cargoSize, int playerMoney) {
		getContentPane().removeAll();
		validate();
		final MarketWindow mw = new MarketWindow
				(marketMap, onship, cargoSize, playerMoney);
		getContentPane().add(mw);
		validate();
		pack();
		setTitle("Solar System Market");
		while ((Integer) mw.getButtonPressed().get("Button") == -1) {
			try {
				Thread.sleep(500);
			} 
			catch(InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		return mw.getButtonPressed();
	}
	
	/**
	 * Pops up the dock bay
	 * 
	 * @param currShipFuel the fuel currently in the ship
	 * @param shipFuelCap fuel capacity of the current ship
	 * @return buttons pressed and fuel bought
	 */
	public int[] dockPop(int currShipFuel, int shipFuelCap) {
		getContentPane().removeAll();
		validate();
		final DockbayWindow dw = new DockbayWindow(currShipFuel, shipFuelCap);
		getContentPane().add(dw);
		validate();
		pack();
		setTitle("Dock Bay");
		while (dw.getButtonPressed()[0] == -1) {
			try {
				Thread.sleep(500);
			} 
			catch(InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		return dw.getButtonPressed();
	}
	
	/**
	 * Pops up the event window
	 * 
	 * @param eventString the string of the event that occurs
	 * @param eventNum the number value of the event
	 * @param eventTitle the string of the title of the event
	 * @return button letting you know that you are to accept your fate
	 */
	public int eventPop(int eventNum, String eventString, String eventTitle) {
		getContentPane().removeAll();
		validate();
		final EventWindow ew = new EventWindow(eventNum, eventString);
		getContentPane().add(ew);
		validate();
		pack();
		setTitle(eventTitle);
		while (ew.getButtonPressed() == -1) {
			try {
				Thread.sleep(500);
			} 
			catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		return ew.getButtonPressed();
	}
	
	/**
	 * Passes market interaction string for use in the CaptainsLog
	 * @param s
	 */
	public void setMarketInteractions(String s){
		pw.setMarketInteractions(s);
	}
}
